<?php
if ( !defined('KAIZEKU') ) { exit(); }
/**
 * WP-iStalker Chrome Min 
 * Query function
 * 
 * @package	WordPress
 * @subpackage	wp-istalker-chrome-min
 * 
 * @category	DatabaseAndUtilities
 * @author	Avice (ChaosKaizer) De'vereux <ck+wp-istalker-min@istalker.net>
 * @author	NH. Noah <noah+wp-istalker-min@kakkoi.net>
 * @copyright 	2007 - 2009 Avice De'vereux, NH. Noah
 * @license 	http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License v2 
 * @version 	CVS: $Id: query.php 13 2009-01-11 15:22:40Z chaoskaizer $
 * @since	0.1
 */


/**
 * is_at()
 * determine wp hirechical section
 * @see $wp_query WP_Query object
 * @param bool $display echo output if true
 * @since 0.1
 */
function is_at($display = false){
	
	$ref = 'is_lost';
	$arr = array('is_home','is_front_page','is_single','is_page','is_category',	
				'is_author','is_tag','is_day','is_month','is_year',
				'is_archive','is_search','is_404');

	foreach ($arr as $k){
		if(call_user_func($k)){
			$ref = $k;
			break;
		}
	}
	
	if ($ref == 'is_single'){
		// attachment;
		if (is_attachment()){
			$ref = 'is_attachment';
		}
	}	
	
	$ref = str_replace('is_','',$ref);	

	if ($display): echo $ref; else: return $ref; endif;
}


/**
 * wpi_is_plugin_active()
 * wrapper for is_plugin_active()
 * 
 * @param	string	$name	The plugin register filename
 * @since	0.1
 */
function wpi_is_plugin_active($plugin_filename){
	
	$plugins = get_option('active_plugins');
	
	if( !is_array($plugins) ) $plugins = array($plugins);
		
	$plugins = array_flip($plugins);
	
	return ( isset($plugins[$plugin_filename]) );
}


/**
 * wpi_get_theme_option()
 * wrapper for get_option()
 * 
 * @see		get_option()	Retrieve option value based on setting name.
 * @param	string	$name	The option name
 * @param	bool	$extra 	Save options with base 64 encoding if true
 * @since	0.1
 */
function wpi_get_theme_option($name, $extra = false){

    $metakey =  WPI_META_PREFIX. ( (!$extra) ? 'settings' : 'extra' );
		
    $options = get_option($metakey);

    if (has_count($options) && isset($options[$name]) ){
    	
    	if ($extra){
    		$options[$name] = call_user_func_array(wpiTheme::DECODE_CONFIG_ENGINE, array($options[$name]) );
    	}

        return $options[$name];
        
    } else {
    	
        wpi_update_theme_options($name, '', $extra);
        return wpi_get_theme_option($name, $extra);
    }
}


/**
 * wpi_option()
 * wrapper for wpi_get_theme_option()
 * 
 * @param	string	$name	The option name
 * @param	bool	$extra 	Save options with base 64 encoding if true
 * @since	0.1
 */
function wpi_option($name, $extra = false){
	return wpi_get_theme_option($name, $extra);
}


/**
 * wpi_update_theme_options()
 * wrapper for update_option()
 * 
 * @see	wpi_get_theme_option()
 * @see	get_option() Retrieve option value based on setting name.
 * @param string $name The option name
 * @param mixed|string $value The option value				
 * @param bool $extra Save options with base 64 encoding if true
 * @since 0.1
 */
function wpi_update_theme_options($name, $value = '', $extra = false){
	
    $metakey =  WPI_META_PREFIX. ( (!$extra) ? 'settings' : 'extra' );
    
    $options = $noptions = get_option($metakey);
    $noptions[$name] = $value;
    
    if ($extra){
    	$noptions[$name] = call_user_func_array(wpiTheme::ENCODE_CONFIG_ENGINE, array($value) );
    }

    if ($options != $noptions) {
        $options = $noptions;
        update_option($metakey, $options);
    }
}
?>